

import { find, instantiate, loader, Prefab, sys } from 'cc';
// import { Alert } from '../game/gameUi/Alert';
import { Wechat } from './Platform/wechat/wechat';
import { EType, IPos, WXCustomAd } from './Platform/wechat/WXCustomAd';

// import { JXDef } from '../conventions/JXCommon';
let adStr = "广告";
/**渠道 */
export enum Channel {
    /**默认 浏览器*/
    DEFAULT = 0,
    /**微信 */
    WECHAT = 1,
    /**头条 抖音 */
    TT = 2,
    /**快手 */
    KS = 3,
    /**oppo渠道 */
    OPPO = 4,
    /**vivo渠道 */
    VIVO = 5,
    /**魅族渠道 */
    MEIZU = 6,
    /**小米渠道 */
    XIAOMI = 7,
    ANDROID,
    IOS,
}
export interface IUserInfo {
    nickName: string;
    avatar: string;
    sex?: number,
    uid?: string,
}
export default class SdkMgr {
    protected static _ins: SdkMgr;

    /**录屏对象 */
    private _mediaRecorder: any = null;
    public static get ins(): SdkMgr {
        if (!this._ins) {
            this._ins = new SdkMgr;
        }
        return this._ins;
    }

    constructor() {
        // if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
        //     this.channel = Channel.TT;
        // } else if (window.kwaigame) {
        //     this.channel = Channel.KS;
        // } else if (sys.platform == sys.Platform.WECHAT_GAME && window.wx) {
        //     this.channel = Channel.WECHAT;
        // } else if (sys.isMobile && sys.isBrowser) {
        //     this.channel = Channel.DEFAULT;
        // } else if (false) {
        //     this.channel = Channel.DEFAULT;
        // }
        if (sys.platform == sys.Platform.WECHAT_GAME) {
            this.channel = Channel.WECHAT;
        }
    }

    private channel: Channel = Channel.DEFAULT;
    private _videoRes: string = "";
    public getChannel() {
        return this.channel;
    }

    /**观看广告 */
    public showRewardAd(des: any, callback: Function, cancel?: Function, fail?: Function) {
        let cb = () => {
            // GameMgr.lUserData.doneTask(ETaskType.watchAd);
            loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
                // if (err) {
                //     console.error(err);
                //     return;
                // }
                // let node = instantiate(prefab);
                // find('Canvas').addChild(node);
                // node.getComponent(Alert).init("广告观看成功");
            })
            callback();
        }

        console.log("观看广告", des);
        switch (this.channel) {
            case Channel.DEFAULT: {
                cb();
                break;
            }
            case Channel.TT: {
                break;
            }
            case Channel.WECHAT: {

                Wechat.showRewardVideoAd().then((isSuc) => {
                    if (isSuc) {
                        console.log("success")
                        this.umaSetPoint(adStr, { way: des });
                        cb();

                    } else if (cancel) {
                        cancel();
                        // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
                        //     if (err) {
                        //         console.error(err);
                        //         return;
                        //     }
                        //     let node = instantiate(prefab);
                        //     find('Canvas').addChild(node);
                        //     node.getComponent(Alert).init("取消观看广告");
                        // })
                    }

                }, (res?) => {
                    if (fail) {
                        fail();
                    }
                    loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
                        // if (err) {
                        //     console.error(err);
                        //     return;
                        // }
                        // let node = instantiate(prefab);
                        // find('Canvas').addChild(node);
                        // node.getComponent(Alert).init("广告加载失败");
                    })
                })
                break;
            }
            case Channel.MEIZU: {
                break;
            }
            case Channel.OPPO: {
                break;
            }
            case Channel.VIVO: {
                break;
            }
            case Channel.XIAOMI: {
                break;
            }
            case Channel.KS: {
                break;
            }
        }

    }

    public umaSetPoint(event: string, obj?: object) {
        return;
        switch (this.channel) {
            case Channel.DEFAULT: {
                break;
            }
            case Channel.TT: {

                tt.uma && tt.uma.trackEvent(event, obj ? obj : null);

                break;
            }
            case Channel.KS: {

                break;
            }
            case Channel.WECHAT: {
                wx.uma && wx.uma.trackEvent(event, obj ? obj : null);
                break;
            }
        }
    }

    public showMoreGamesModal() {
        switch (this.channel) {
            case Channel.TT: {
                break;
            }
        }
    }

    public ttFocusOn() {
    }

    /**录屏开始 */
    protected startRecoding() {

    }

    /**事件添加开始录屏 */
    public recordStart() {
        console.log("录屏开始*--*");
    }

    public puaseRecord() {
        console.log("暂停录屏");
    }

    public resumeRecord() {
        console.log("继续录屏")
    }

    /**事件添加结束录屏 */
    public recordEnd() {
        console.log("录屏结束*--*");
    }

    /**事件转发 */
    public recordCut(cb: Function) {
    }
    //授权
    public authorize(cb: (userInfo: IUserInfo | null) => void) {
        if (this.channel == Channel.DEFAULT) {
            cb(null);
        } else if (this.channel == Channel.WECHAT) {
            // Wechat.login(() => {

            //     Wechat.getUserInfo((res: any) => {
            //         let userInfo: IUserInfo = {
            //             nickName: res.userInfo.nickName,
            //             avatar: res.userInfo.avatarUrl,
            //         }
            //         cb(userInfo);
            //     }, (res) => {
            //         console.log(`getUserInfo 调用失败`);
            //         console.log(res);
            //         cb(null);
            //     })
            // }, (res) => {
            //     log(res);
            //     cb(null);
            // });
            let button = wx.createUserInfoButton({
                type: 'text',
                text: '获取用户信息',
                style: {
                    left: 10,
                    top: 76,
                    width: 200,
                    height: 40,
                    lineHeight: 40,
                    backgroundColor: '#ff0000',
                    color: '#ffffff',
                    textAlign: 'center',
                    fontSize: 16,
                    borderRadius: 4
                }
            })
            button.onTap((res) => {
                console.log(res.userInfo.avatarUrl);
                console.log(res.userInfo.nickName);
            })

        } else {
            cb(null);
        }
    }
    public initGame() {

    }

    public loginOut() {
    }
    /**是否显示分享录屏按钮 */
    public isShowRecordBtn() {
        if (this.channel == Channel.TT || this.channel == Channel.KS) {
            return true;
        }
        return false;
    }

    public isWechat() {
        return this.channel == Channel.WECHAT;
    }
    public preLoadAd() {
        if (this.channel == Channel.WECHAT) {
            Wechat.preLoadAd();
        }
    }
    //显示插屏广告
    public showIntersAd(closeCb?: Function) {
        if (this.channel == Channel.WECHAT) {
            Wechat.showInterstitialAd(closeCb);
        }

    }

    public showBanner() {
        if (this.channel == Channel.WECHAT) {
            Wechat.showBannerAd(4000);
        }
    }
    public showBannerTop() {
        if (this.channel == Channel.WECHAT) {
            Wechat.showBannerAd(4000);
        }
    }
    public showBannerTurns(time) {
        if (this.channel == Channel.WECHAT) {
            Wechat.showBannerTurns(time);
        }
    }
    public hideBanner() {
        if (this.channel == Channel.WECHAT) {
            Wechat.hideBannerAd();
        }
    }
    public hideBannerTop() {
        if (this.channel == Channel.WECHAT) {
            Wechat.hideBannerAd();
        }
    }
    public showWxCustomAd(flag, type: EType, pos: IPos, extraId?) {
        if (this.channel == Channel.WECHAT) {
            WXCustomAd.createCustomAd(flag, type, pos, extraId);
        }
    }
    public hideWxCustomAd(flag) {
        if (this.channel == Channel.WECHAT) {
            WXCustomAd.hideCustomAd(flag);
        }
    }
    public hideAllWxCustom() {
        if (this.channel == Channel.WECHAT) {
            WXCustomAd.hideAllAd();
        }
    }
}
